<?php namespace App\Services;

use App\Facades\Infra\CustomerManager;
use Auth;
use App\Exceptions\DataBaseException;
use Illuminate\Database\QueryException;

class CustomerInfoService{

	/**
	 * @Author   Leikes
	 * @DateTime 2018-05-15
	 * @desc     添加用户信息
	 * @param    [type]     $data [description]
	 * @return   [type]           [description]
	 */
	public function createInfo($data){
		$createData = [
			'header' => $data['header'],
			'name' => $data['name'],
			'sex' => $data['sex'],
			'customer_id' => Auth::user()->id
		];

		try {
			return CustomerManager::createInfo($createData);
		} catch (QueryException $e) {
			throw new DataBaseException();
		}
		
	}

	/**
	 * @Author   Leikes
	 * @DateTime 2018-05-15
	 * @desc     获取用户信息
	 * @return   [type]     [description]
	 */
	public function getInfo(){

		$conditions = [
			'phone' => Auth::user()->phone
		];

		return CustomerManager::getInfoByCustomer($conditions,$first = false);
	}

	/**
	 * @Author   Leikes
	 * @DateTime 2018-05-15
	 * @desc     修改用户信息
	 * @param    [type]     $data [description]
	 * @return   [type]           [description]
	 */
	public function updateInfo($data){
		$conditions = [
			'customer_id' => Auth::user()->id
		];

		$info = CustomerManager::getCustomerInfos($conditions,$first = false);

		$updateData = [
			'header' => $data['header'],
			'name' => $data['name'],
			'sex' => $data['sex'],
		];

		return CustomerManager::updateInfo($info,$updateData);
	}

}